package com.liu.es.test;

import org.apache.http.HttpHost;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;

/**
 * 批量操作的对象:BulkRequest
 */
public class ESTest_Doc_Delete_Batch {
    public static void main(String[] args) throws Exception {

        RestHighLevelClient esClient = new RestHighLevelClient(
                RestClient.builder(new HttpHost("localhost", 9200, "http"))
        );

        //批量插入数据
        //创建批量对象
        BulkRequest request = new BulkRequest();

        //往批量操作对象添加数据
        //add函数里接受DeleteRequest、IndexRequest，其中这两个对象都需要连续调用index()与id()这两个方法来绑定操作的文档
        //add的作用或许就是添加操作，无论是添加还是删除

        request.add(new DeleteRequest().index("user").id("1001"));
        request.add(new DeleteRequest().index("user").id("1002"));
        request.add(new DeleteRequest().index("user").id("1003"));


        //此处或许是发送上面的操作指令，并接受返回结果
        BulkResponse responses = esClient.bulk(request, RequestOptions.DEFAULT);

        System.out.println(responses.getItems());
        System.out.println(responses.getIngestTookInMillis());

        esClient.close();
    }
}
